package com.wws.family.dialog

import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.github.gzuliyujiang.wheelpicker.impl.UnitDateFormatter
import com.github.gzuliyujiang.wheelpicker.widget.DateWheelLayout
import com.wika.basics.base.BaseDialog
import com.wika.basics.widget.TipsToast
import com.wika.framework.manager.AppManager
import com.wws.family.databinding.DialogNoteBookBinding
import com.wws.family.utils.DateUtils
import com.wws.family.utils.PublicUtils
import java.text.DecimalFormat

/**
 *@Description: 记事本输入框
 *@Date: 2024/7/26 16:04
 *@Author: WangWeiShuo
 */
class NoteBookInputDialog {
    class Builder(
        context: Activity
    ) : BaseDialog.Builder<Builder>(context) {
        private val mBinding: DialogNoteBookBinding = DialogNoteBookBinding.inflate(LayoutInflater.from(context))
        private var onSaveListener: OnSaveListener? = null
        private var commTime: String? = null
        private val decimalFormat = DecimalFormat("00")

        init {
            setContentView(mBinding.root)
            val width = AppManager.getInstance(context).getScreenWidthPx()
            setWidth((width * 0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.IOS)
            setGravity(Gravity.CENTER)
            showContent()
        }


        private fun showContent(){
            mBinding.btnSure.setOnClickListener {
                val name = mBinding.etCommName.text.toString()
                val remark = mBinding.etCommRemark.text.toString()

                if(name.isNullOrEmpty()){
                    TipsToast.showWarningTips("请输入名称")
                }
                if(commTime.isNullOrEmpty()){
                    TipsToast.showWarningTips("请选择时间")
                }

                dismiss()
                if (onSaveListener != null){
                    onSaveListener?.onSave(name, commTime!!,remark)
                }
            }
            mBinding.btnCancel.setOnClickListener {
                dismiss()
            }

            mBinding.tvCommTime.setOnClickListener {
                selectTime()
            }
        }

        fun addSaveListener(listener: OnSaveListener): Builder{
            onSaveListener = listener
            return this
        }

        private fun selectTime() {
            PublicUtils.setArrowDownUp(mBinding?.ivArrowTime!!,false)
            val picker = DatePicker(context as Activity)
            //picker.setBodyWidth(240);
            val wheelLayout: DateWheelLayout = picker.wheelLayout
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            picker.setOnDatePickedListener { year, month, day ->
                commTime = "$year-${decimalFormat.format(month)}-${decimalFormat.format(day)}"
                val week = DateUtils.obtainWeek(commTime,DateUtils.YYYYMMDD)
                mBinding.tvCommTime.text = commTime + "(${week})"
            }
            picker.setOnDismissListener {
                PublicUtils.setArrowDownUp(mBinding.ivArrowTime,true)
            }
            //wheelLayout.setDateLabel("年", "月", "日");
            wheelLayout.setDateFormatter(UnitDateFormatter())
            //wheelLayout.setRange(DateEntity.target(2021, 1, 1), DateEntity.target(2050, 12, 31), DateEntity.today());
            wheelLayout.setRange(DateEntity.target(2025, 1, 1), DateEntity.yearOnFuture(5), DateEntity.today())
            picker.wheelLayout.setResetWhenLinkage(false)
            picker.show()
        }
    }

    interface OnSaveListener{
        fun onSave(name: String,time: String,remark:String)
    }
}